<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AlterTableMeterTypesAddResetColumn extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('meter_types', function (Blueprint $table) {
            if (Schema::hasColumn('meter_types', 'max_consumption') == false) {
                $table->integer('max_consumption')->default(1000)->after('unit');
            }
            if (Schema::hasColumn('meter_types', 'can_reset') == false) {
                $table->boolean('can_reset')->default(true)->after('max_consumption');
            }
            if (Schema::hasColumn('meter_types', 'reset_limit') == false) {
                $table->unsignedInteger('reset_limit')->default(100000)->after('can_reset');
            }
        });
        DB::table('meter_types')->where('id', 1)->update(['max_consumption' => 1000]); //水
        DB::table('meter_types')->where('id', 2)->update(['max_consumption' => 2500]); //电
        DB::table('meter_types')->where('id', 3)->update(['max_consumption' => 500]); //热水
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('meter_types', function (Blueprint $table) {
            if (Schema::hasColumn('meter_types', 'max_consumption')) {
                $table->dropColumn('max_consumption');
            }
            if (Schema::hasColumn('meter_types', 'reset_limit')) {
                $table->dropColumn('reset_limit');
            }
            if (Schema::hasColumn('meter_types', 'can_reset')) {
                $table->dropColumn('can_reset');
            }
        });
    }
}
